﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NRCP.BusinessLogicLayer;
using NRCP.BusinessEntities;
using NRCP.Helpers;
using Imgalib.Utility;

namespace NRCP
{
    public partial class Plan : NationalPlanBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.PresentPageStep = NationalPlanSteps.Initiate;
                this.CheckSteps();

                //respMessage.SetNoticeMessage("Please Select A Year To Start Creating New Plan. You Have To Choose Fish Type, Participating Test Groups And Division For Creating Plan.");
                LoadData();
            }
        }

        private void LoadData()
        {
            ddlNationalPlanYear.DataSource = Enumerable.Range(2008, 8).ToList();
            ddlNationalPlanYear.DataBind();

            FishBLL bll = new FishBLL();
            chkFishList.DataSource=bll.GetAllFish();
            chkFishList.DataTextField = "Name";
            chkFishList.DataValueField = "Id";
            chkFishList.DataBind();

            ChemicalGroupBLL chemicalGroupBll = new ChemicalGroupBLL();
            chkTestGroup.DataSource = chemicalGroupBll.GetAllChemicalGroups();
            chkTestGroup.DataTextField = "Name";
            chkTestGroup.DataValueField = "Id";
            chkTestGroup.DataBind();

            DivisionBLL divisionbll = new DivisionBLL();
            chkDivision.DataSource = divisionbll.GetAllDivisions();
            chkDivision.DataTextField = "Name";
            chkDivision.DataValueField = "Id";
            chkDivision.DataBind();
        }

        protected void lnbSave_Click(object sender, EventArgs e)
        {
            try
            {
                NationalPlanBLL nationalPlanBll = new NationalPlanBLL();
                NationalPlan plan = new NationalPlan();
                plan.CreateDate = DateTime.Now;
                plan.CreatedBy = SessionManager.LOGGEDIN_USER_NAME;
                plan.PlanYear = Convert.ToInt32(ddlNationalPlanYear.SelectedItem.Value);
                plan.Status = PlanStatus.Pending.ToString();
                plan.ApprovalStatus = ApprovalStatus.NotApproved.ToString();
                plan.CompletedSteps = CompletedSteps.Configure.ToString();
                Int32 planId = nationalPlanBll.CreateNewNationalPlan(plan);
                if (planId > 0)
                {
                    NationalPlanChemicalGroupBLL nationalPlanChemicalGroupBll = new NationalPlanChemicalGroupBLL();
                    foreach (ListItem item in chkTestGroup.Items)
                    {
                        if (item.Selected)
                        {
                            NationalPlanChemicalGroup nationalPlanChemicalGroup = new NationalPlanChemicalGroup();
                            nationalPlanChemicalGroup.NationalPlanlId = planId;
                            nationalPlanChemicalGroup.ChemicalGroupId = Convert.ToInt32(item.Value);
                            nationalPlanChemicalGroupBll.CreateNewNationalPlanChemicalGroup(nationalPlanChemicalGroup);
                        }
                    }

                    NationalPlanFishBLL nationalPlanFishBll = new NationalPlanFishBLL();
                    
                    foreach (ListItem item in chkFishList.Items)
                    {
                        if (item.Selected)
                        {
                            NationalPlanFish nationalPlanFish = new NationalPlanFish();
                            nationalPlanFish.NationalPlanId = planId;
                            nationalPlanFish.FishId = Convert.ToInt32(item.Value);
                            nationalPlanFishBll.CreateNewNationalPlanFish(nationalPlanFish);
                        }
                    }

                    NationalPlanDivisionBLL nationalPlanDivisionBll = new NationalPlanDivisionBLL();
                    foreach (ListItem item in chkDivision.Items)
                    {
                        if (item.Selected)
                        {
                            NationalPlanDivision nationalPlanDivision = new NationalPlanDivision();
                            nationalPlanDivision.NationalPlanId = planId;
                            nationalPlanDivision.DivisionId = Convert.ToInt32(item.Value);
                            nationalPlanDivisionBll.CreateNewNationalPlanDivision(nationalPlanDivision);
                        }
                    }

                    HttpContext.Current.Response.Redirect(SiteSettings.BaseUrl + "national-plan.aspx?params=" + Cryptography.Encrypt(planId.ToString()));
                }
                else
                {
                    respMessage.SetFailMessage("Failed To Create National Plan");
                }
            }
            catch(Exception ex)
            {
                respMessage.SetFailMessage("Failed To Create National Plan"+ex.Message);
            }
        }
    }
}